package com.btkjgc.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.models.AjaxObj;
import com.btkjgc.models.ElectricChargingSite;
import com.btkjgc.models.extend.ElectricChargingSiteModel;
import com.btkjgc.service.intf.ElectricChargingSiteManageService_Intf;
import com.btkjgc.utils.HibernateUtil;
import com.btkjgc.utils.PinYin2AbbreviationUtil;

@Controller
public class ElectricChargingSiteManageController {

	static Logger logger = Logger.getLogger(ElectricChargingSiteManageController.class);

    @Autowired
	private ElectricChargingSiteManageService_Intf electricChargingSiteManageService;

	@RequestMapping(value = "electric_charging_site_manage", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView getBusinessRecords(HttpServletRequest request,ElectricChargingSiteModel electricChargingSiteModel) {

		ModelAndView mv = new ModelAndView("electric_charging_site_manage");
		getElectricChargingSiteList(request, mv,electricChargingSiteModel);
		return mv;
	}

	@RequestMapping(value = "edit_electric_charging_site")
	@ResponseBody
	public AjaxObj editElectricChargingSite(ElectricChargingSite electricChargingSite)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

		AjaxObj ajaxObj=electricChargingSiteManageService.updateElectricChargingSiteModel(electricChargingSite);

		return ajaxObj;
	}

	@RequestMapping(value = "change_state")
	public String changeState(ElectricChargingSite electricChargingSite)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {

	    electricChargingSiteManageService.updateElectricChargingSiteModel(electricChargingSite);

		return "redirect:/electric_charging_site_manage";
	}
	
 
	@SuppressWarnings("unchecked")
	private void getElectricChargingSiteList(HttpServletRequest request, ModelAndView mv,ElectricChargingSiteModel electricChargingSiteModel) {
 
		String page = request.getParameter("page");
		String sortfield = request.getParameter("sortfield");
		if (StringUtils.isEmpty(sortfield)) {
			sortfield = "createdate";
		}

		int pageIndex = 0;
		if (page != null) {
			pageIndex = Integer.parseInt(page);
		}

		List<Object[]> paramList = new ArrayList<Object[]>();
		Object[] arr1 = { "ecst.name","like", electricChargingSiteModel.getName()};
		Object[] arr2 = { "ecst.state", "=", electricChargingSiteModel.getState() };
		paramList.add(arr1);
		paramList.add(arr2);
		

		String sql ="SELECT\n" +
				"	ecst.*, pct. NAME properCompanyName,\n" +
				"	concat(ct1.`Name`, ct2.`Name`) areaName,\n" +
				"         CASE ecst.state\n" +
				"            WHEN 1 THEN '运营中'\n" +
				"            WHEN 2 THEN '已关闭'\n" +
				"            WHEN 3 THEN '维修中'\n" +
				"         END stateName,\n" +
				"         CASE ecst.supersite\n" +
				"            WHEN 1 THEN '是'\n" +
				"            WHEN 0 THEN '否'\n" +
				"         END supersiteName\n" +
				"FROM\n" +
				"	electric_charging_site_t ecst\n" +
				"LEFT JOIN property_company_t pct ON ecst.proper_companyid = pct.id AND pct.del = 0\n" +
				"LEFT JOIN cities_t ct1 ON ct1.id = ecst.cityid\n" +
				"LEFT JOIN cities_t ct2 ON ct2.id = ecst.areaid\n" +
				"WHERE\n" +
				"	ecst.del = 0";

		sql = electricChargingSiteManageService.getwhereSQL(sql, paramList, sortfield);

		int totalRecord = electricChargingSiteManageService.getCount(sql, paramList);

		// 获取分页查询的数据结果
		List<ElectricChargingSiteModel> electricChargingSiteModelList = electricChargingSiteManageService.getList(
				ElectricChargingSiteModel.class, paramList, sql, pageIndex, 15, sortfield);

		
		for (ElectricChargingSiteModel electricChargingSiteModel2 : electricChargingSiteModelList) {
			electricChargingSiteModel2.setPinyin_name(PinYin2AbbreviationUtil.cn2py(electricChargingSiteModel2.getName()));
		}
		
		// 设置后台用户界面所需的其他数据
		electricChargingSiteManageService.setListData(request, mv, pageIndex, totalRecord,
				electricChargingSiteModelList);

	}

	@RequestMapping(value = "get_electric_charging_site_by_id")
	@ResponseBody
	public ElectricChargingSiteModel getElectricChargingSiteById(Integer id) {

		String sql = "SELECT *\n"
				+ "FROM\n"
				+ "electric_charging_site_t ecst\n"
				+ "WHERE 	ecst.id = ? and ecst.del=0";

		ElectricChargingSiteModel electricChargingSiteModel = (ElectricChargingSiteModel) HibernateUtil
				.executeSQLQueryObject(sql, ElectricChargingSiteModel.class, id);

		return electricChargingSiteModel;
	}

	@RequestMapping(value = "add_electric_charging_site/{a}")
	@ResponseBody
	public   AjaxObj jsonAddElectricChargingSite(HttpServletRequest request,
			@PathVariable("a") int a, ElectricChargingSiteModel electricChargingSiteModel) {
 
		AjaxObj ajaxObj = null;
		if (electricChargingSiteModel.getName().length() > 0) {
 
			  ajaxObj = electricChargingSiteManageService
					.insertElectricChargingSiteModel(electricChargingSiteModel);
			

		}
		return ajaxObj;
	}


}
